package com.baigt.jvm.classloader;

import java.util.concurrent.TimeUnit;

/**
 * 类功能描述:
 *
 * @author baigt
 * @version V1.0
 * @since 2019-06-16 09:46
 */
public class RickyClassCloaderUsedDemo {
    public static void main01(String[] args) throws Exception {
        RickyClassCloader loader1=new RickyClassCloader("ricky");
        loader1.setPath("d:\\");
        Class<?> clz1 = loader1.loadClass("com.ricky.jvm.classloader.RickyClassCloaderEntity");
        RickyClassCloader loader2=new RickyClassCloader("ricky");
        Class<?> clz2 = loader2.loadClass("com.baigt.jvm.classloader.RickyClassCloaderEntity");
        // 结果是什么？
        System.out.println(clz1==clz2);
        Object o = clz1.newInstance();
        System.out.println(o.getClass().getClassLoader());
    }

    /**
     * copy项目中的class到 course2目录中
     * 删除course中编译的class文件
     * @param args
     * @throws Exception
     */
    public static void main02(String[] args) throws Exception {
        RickyClassCloader loader1=new RickyClassCloader("ricky");
        loader1.setPath("D:\\IdeaProjects\\course2\\");
        Class<?> clz1 = loader1.loadClass("com.baigt.jvm.classloader.RickyClassCloaderEntity");
        RickyClassCloader loader2=new RickyClassCloader("ricky");
        loader2.setPath("D:\\IdeaProjects\\course2\\");
        Class<?> clz2 = loader2.loadClass("com.baigt.jvm.classloader.RickyClassCloaderEntity");
        // 结果是什么？
        System.out.println(clz1==clz2);
        Object o = clz1.newInstance();
        System.out.println(o.getClass().getClassLoader());
        TimeUnit.MINUTES.sleep(10);
    }

    /**
     * main 2方法执行结果


     C:\Users\Administrator>jps
     560 Jps
     11236
     12004
     4228 RickyClassCloaderUsedDemo
     13384 Main
     15452 Launcher
     9244 GCDeadLockDemo

     C:\Users\Administrator>jmap -clstats 4228
     Attaching to process ID 4228, please wait...
     Debugger attached successfully.
     Server compiler detected.
     JVM version is 25.45-b02
     finding class loader instances ..done.
     computing per loader stat ..done.
     please wait.. computing liveness...........done.
     class_loader    classes bytes   parent_loader   alive?  type

     <bootstrap>     618     1153749   null          live    <internal>
     0x000000076b3a57f8      35      113034  0x000000076b38e560      live    sun/misc/Launcher$AppClassLoader@0x00000007c000f668
     0x000000076b5fa598      2       1896    0x000000076b3a57f8      live    com/ricky/jvm/classloader/RickyClassCloader@0x00000007c0060a18
     0x000000076b38e560      0       0         null          live    sun/misc/Launcher$ExtClassLoader@0x00000007c000fa10
     0x000000076b9b0ea0      0       0       0x000000076b3a57f8      live    java/util/ResourceBundle$RBClassLoader@0x00000007c0067c78
     0x000000076b5e7690      2       1976    0x000000076b3a57f8      live    com/ricky/jvm/classloader/RickyClassCloader@0x00000007c0060a18

     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        main01(null);
//        main02(null);
    }
}
